package modbus

import "gitee.com/lossage/simplego/math/crc"

type ModbusRTUAnalysis struct {
	ModbusAnalysis
}

func (_this *ModbusRTUAnalysis) Start() bool {
	_this.unpackLinkLayer = _this.unpackLinkLayerFun
	_this.packLinkLayer = _this.packLinkLayerFun
	return _this.ModbusAnalysis.Start()
}

func (_this *ModbusRTUAnalysis) packLinkLayerFun(buffer []uint8) []uint8 {
	return crc.Crc16Add(buffer)
}

func (_this *ModbusRTUAnalysis) unpackLinkLayerFun(buffer []uint8) []uint8 {
	if !crc.Crc16Check(buffer) {
		// logger.Info("crc校验失败")
		return nil
	} else {
		return buffer[0 : len(buffer)-2]
	}
}
